package com.btkjgc.framework;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.controller.BaseController;
import com.btkjgc.models.AdminUser;
import com.btkjgc.utils.DataDisplayUtil;
import com.btkjgc.utils.FtlOperationUtil;
import com.btkjgc.utils.LogUtils;

public class BasicController extends BaseController
{
	static Logger logger = Logger.getLogger(BasicController.class);

	Class<?> updateForm;

	Method updateMethod;

	Method deleteMethod;

	Class<?> insertForm;

	Method insertMethod;

	Method listMethod;

	public BasicController()
	{
		Method[] ms = this.getClass().getMethods();
		for (Method m : ms)
		{
			if (m.getName() == "insert")
			{
				insertMethod = m;
				insertForm = m.getParameterTypes()[0];
			}
			if (m.getName() == "delete")
			{
				deleteMethod = m;
			}
			if (m.getName() == "update")
			{
				updateMethod = m;
				updateForm = m.getParameterTypes()[0];
			}
			if (m.getName() == "list")
			{
				listMethod = m;
			}
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView process(HttpServletRequest request)
	{
		ModelAndView mv = new ModelAndView();

		mv.addObject("DisplayUtil", new DataDisplayUtil());
		mv.addObject("FtlOperationUtil", new FtlOperationUtil());
		AdminUser adminUser = (AdminUser) request.getSession().getAttribute("user");
		mv.addObject("user_info", adminUser);

		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements())
		{
			String name = names.nextElement();
			mv.addObject(name, request.getParameter(name));
		}

		Map<String, Map<String, Boolean>> functionMap = (Map<String, Map<String, Boolean>>) request
				.getSession().getAttribute("role_function");
		if (functionMap != null)
		{
			Map<String, Boolean> map = functionMap.get(request.getRequestURI());
			if (map != null)
			{
				for (String func : map.keySet())
				{
					if (map.get(func) != null)
					{
						mv.addObject(func, true);
					}
				}
			}
		}

		try
		{

			String postaction = request.getParameter("postaction");
			if (postaction != null && postaction.length() > 0)
			{
				if ("delete".equals(postaction))
				{
					String postarg = request.getParameter("postarg");
					deleteMethod.invoke(this, request, mv, postarg);
				}
				if ("insert".equals(postaction))
				{
					if (insertMethod != null)
					{
						Class form = insertMethod.getParameterTypes()[2];
						Object formarg = form.newInstance();
						fillBean(request, mv, formarg);
						insertMethod.invoke(this, request, mv, formarg);
					}
				}
				if ("update".equals(postaction))
				{
					String postarg = request.getParameter("postarg");
					if (updateMethod != null)
					{
						Class form = updateMethod.getParameterTypes()[2];
						Object formarg = form.newInstance();
						PropertyUtils.setProperty(formarg, "id", postarg);
						fillBean(request, mv, formarg);
						updateMethod.invoke(this, request, mv, formarg);
					}
				}
			}
			if (listMethod != null)
			{
				Class form = listMethod.getParameterTypes()[2];
				Object formarg = form.newInstance();
				fillBean(request, mv, formarg);
				listMethod.invoke(this, request, mv, formarg);
			}
		}
		catch (Exception ex)
		{
			logger.error("发生错误", ex);
		}

		return mv;
	}

	public void fillBean(HttpServletRequest request, ModelAndView mv,
			Object formarg) throws IllegalAccessException,
			InvocationTargetException, NoSuchMethodException
	{
		PropertyDescriptor[] properties = PropertyUtils
				.getPropertyDescriptors(formarg);
		for (PropertyDescriptor p : properties)
		{
			if (p.getName().equals("id") || p.getName().equals("class"))
			{
				continue;
			}
			Object v = request.getParameter(p.getName());
			if (v == null)
			{
				mv.addObject(p.getName(), "");
			}
			else
			{
				if (p.getPropertyType() == Integer.class)
				{
					// if(v !=null &&v.toString().length()>0){
					Integer value = Integer.parseInt(v.toString());
					PropertyUtils.setProperty(formarg, p.getName(), value);
					// }
				}
				else if (p.getPropertyType() == Float.class)
				{
					Float value = Float.parseFloat(v.toString());
					PropertyUtils.setProperty(formarg, p.getName(), value);
				}
				else if (p.getPropertyType() == Double.class)
				{
					Double value = Double.parseDouble(v.toString());
					PropertyUtils.setProperty(formarg, p.getName(), value);
				}
				else
				{
					PropertyUtils.setProperty(formarg, p.getName(), v);
				}
			}
		}
		LogUtils.dump(formarg);
	}
}
